package com.fyy.socket.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Author fyy
 * @Description
 * @Date 2020/12/10 11:03
 */
public class LoginServer {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //开启服务,指定监听端口
        ServerSocket socketServer = new ServerSocket(8080);
        //在等待的接口监听
        Socket socket = socketServer.accept(); //等待客户端连接，没有连接程序在此阻塞
//        DataInputStream dis = new DataInputStream(socket.getInputStream());
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
        //接收数据
        User user = (User) ois.readObject();
        System.out.println("用户名:" + user.getUserId());
        System.out.println("密码:" + user.getPassword());
        //响应请求
        if(user.getUserId().equals("admin") && user.getPassword().equals("123")){
            dos.writeUTF("用户登录成功！");
        }else{
            dos.writeUTF("用户名/密码错误！");
        }
        //关闭资源
        ois.close();
        dos.close();
        //socket.close(); //不需要手动关闭
        socketServer.close(); //需要手动关闭
    }

}
